package cn.nebula.music.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.nebula.music.domain.Album;
import cn.nebula.music.domain.Classification;
import cn.nebula.music.domain.Combination;
import cn.nebula.music.domain.Comment;
import cn.nebula.music.domain.Mv;
import cn.nebula.music.domain.Picture;
import cn.nebula.music.domain.Singer;
import cn.nebula.music.domain.Song;
import cn.nebula.music.domain.Songlist;
import cn.nebula.music.domain.User;
import cn.nebula.music.service.AlbumService;
import cn.nebula.music.service.ClassificationService;
import cn.nebula.music.service.CombinationService;
import cn.nebula.music.service.CommentService;
import cn.nebula.music.service.MvService;
import cn.nebula.music.service.PictureService;
import cn.nebula.music.service.SingerService;
import cn.nebula.music.service.SongService;
import cn.nebula.music.service.SonglistService;
import cn.nebula.music.service.UserService;

@Controller
public class ClientController {

	@Autowired
	private MvService mvService;

	@Autowired
	private SingerService singerService;

	@Autowired
	private PictureService pictureService;

	@Autowired
	private CombinationService combinationService;

	@Autowired
	private ClassificationService classificationService;

	@Autowired
	private AlbumService albumService;

	@Autowired
	private SongService songService;

	@Autowired
	private UserService userService;

	@Autowired
	private CommentService commentService;

	@Autowired
	private SonglistService songlistService;

	@RequestMapping("/mvLists")
	@ResponseBody
	public Map<String, Object> mvLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("mvs", mvService.findAllMvsOfBT());
		return values;
	}

	@RequestMapping("/chMvLists")
	@ResponseBody
	public Map<String, Object> chMvLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Mv> chMvs = new HashSet<Mv>();
		for (Mv mv : mvService.findAllMvsOfBT()) {
			if (mv != null && mv.getSong() != null && (mv.getSong().getLanguage().equals("华语")||mv.getSong().getLanguage().equals("粤语")))
				chMvs.add(mv);
		}
		values.put("mvs", chMvs);
		return values;
	}

	@RequestMapping("/westMvLists")
	@ResponseBody
	public Map<String, Object> westMvLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Mv> westMvs = new HashSet<Mv>();
		for (Mv mv : mvService.findAllMvsOfBT()) {
			if (mv != null && mv.getSong() != null && mv.getSong().getLanguage().equals("英语"))
				westMvs.add(mv);
		}
		values.put("mvs", westMvs);
		return values;
	}

	@RequestMapping("/japKorMvLists")
	@ResponseBody
	public Map<String, Object> japKorMvLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Mv> japKorMvs = new HashSet<Mv>();
		for (Mv mv : mvService.findAllMvsOfBT()) {
			if (mv != null && mv.getSong() != null &&( mv.getSong().getLanguage().equals("日语")||mv.getSong().getLanguage().equals("韩语")))
				japKorMvs.add(mv);
		}
		values.put("mvs", japKorMvs);
		return values;
	}

	@RequestMapping("/chWSingerLists")
	@ResponseBody
	public Map<String, Object> chWSingerLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Singer> chWSingers = new HashSet<Singer>();
		for (Singer singer : singerService.findAllSingersWP()) {
			if (singer.getNationality().equals("中国") && singer.getSex().equals("女") && singer != null)
				chWSingers.add(singer);
		}

		values.put("singers", chWSingers);
		return values;
	}

	@RequestMapping("/chMSingerLists")
	@ResponseBody
	public Map<String, Object> chMSingerLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Singer> chMSingers = new HashSet<Singer>();
		for (Singer singer : singerService.findAllSingersWP()) {
			if (singer.getNationality().equals("中国") && singer.getSex().equals("男") && singer != null)
				chMSingers.add(singer);
		}

		values.put("singers", chMSingers);
		return values;
	}

	@RequestMapping("/westWSingerLists")
	@ResponseBody
	public Map<String, Object> westWSingerLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Singer> westWSingers = new HashSet<Singer>();

		for (Singer singer : singerService.findAllSingersWP()) {
			if (singer.getSex().equals("女") && (!singer.getNationality().equals("中国"))
					&& (!singer.getNationality().equals("日本")) && (!singer.getNationality().equals("韩国"))
					&& singer != null)
				westWSingers.add(singer);
		}

		values.put("singers", westWSingers);
		return values;
	}

	@RequestMapping("/westMSingerLists")
	@ResponseBody
	public Map<String, Object> westMSingerLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Singer> westMSingers = new HashSet<Singer>();

		for (Singer singer : singerService.findAllSingersWP()) {
			if (singer.getSex().equals("男") && (!singer.getNationality().equals("中国"))
					&& (!singer.getNationality().equals("日本")) && (!singer.getNationality().equals("韩国"))
					&& singer != null)
				westMSingers.add(singer);
		}

		values.put("singers", westMSingers);
		return values;
	}

	@RequestMapping("/japKorMSingerLists")
	@ResponseBody
	public Map<String, Object> japKorMSingerLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();

		Set<Singer> japWKorMSingers = new HashSet<Singer>();
		for (Singer singer : singerService.findAllSingersWP()) {
			if (singer.getSex().equals("男")
					&& (singer.getNationality().equals("日本") || singer.getNationality().equals("韩国")) && singer != null)
				japWKorMSingers.add(singer);
		}

		values.put("singers", japWKorMSingers);
		return values;
	}

	@RequestMapping("/japKorWSingerLists")
	@ResponseBody
	public Map<String, Object> japKorWSingerLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();

		Set<Singer> japWKorSingers = new HashSet<Singer>();
		for (Singer singer : singerService.findAllSingersWP()) {
			if (singer.getSex().equals("女")
					&& (singer.getNationality().equals("日本") || singer.getNationality().equals("韩国")) && singer != null)
				japWKorSingers.add(singer);
		}

		values.put("singers", japWKorSingers);
		return values;
	}

	@RequestMapping("/chCombinationLists")
	@ResponseBody
	public Map<String, Object> chCombinationLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Combination> chCombinations = new HashSet<Combination>();
		for (Combination combination : combinationService.findAllCombinations()) {
			if (combination != null)
				for (Classification classification : combination.getClassifications())
					if (classification.getName().equals("华语组合")) {
						chCombinations.add(combination);
						break;
					}
		}
		values.put("singers", chCombinations);
		return values;
	}

	@RequestMapping("/westCombinationLists")
	@ResponseBody
	public Map<String, Object> westCombinationLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Combination> westCombinations = new HashSet<Combination>();
		for (Combination combination : combinationService.findAllCombinations()) {
			if (combination != null)
				for (Classification classification : combination.getClassifications())
					if (classification.getName().equals("欧美组合")) {
						westCombinations.add(combination);
						break;
					}
		}
		values.put("singers", westCombinations);
		return values;
	}

	@RequestMapping("/japKorCombinationLists")
	@ResponseBody
	public Map<String, Object> japKorCombinationLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Combination> japKorCombinations = new HashSet<Combination>();

		for (Combination combination : combinationService.findAllCombinations()) {
			if (combination != null)
				for (Classification classification : combination.getClassifications())
					if (classification.getName().equals("日韩组合")) {
						japKorCombinations.add(combination);
						break;
					}
		}
		values.put("singers", japKorCombinations);
		return values;
	}

	@RequestMapping("/albumLists")
	@ResponseBody
	public Map<String, Object> albumLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("albums", albumService.findAllAlbums());
		return values;
	}

	@RequestMapping("/chAlbumLists")
	@ResponseBody
	public Map<String, Object> chAlbumLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Album> chAlbums = new HashSet<Album>();
		for (Album album : albumService.findAllAlbumsOfBT()) {
			for (Singer singer : album.getSingers())
				if (singer.getNationality().equals("中国")) {
					chAlbums.add(album);
					break;
				}
		}

		values.put("albums", chAlbums);
		return values;
	}

	@RequestMapping("/westAlbumLists")
	@ResponseBody
	public Map<String, Object> westAlbumLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Album> westAlbums = new HashSet<Album>();

		for (Album album : albumService.findAllAlbums()) {
			for (Singer singer : album.getSingers())
				if ((!singer.getNationality().equals("中国")) && (!singer.getNationality().equals("日本"))
						&& (!singer.getNationality().equals("韩国"))) {
					westAlbums.add(album);
					break;
				}
		}

		values.put("albums", westAlbums);
		return values;
	}

	@RequestMapping("/japKorAlbumLists")
	@ResponseBody
	public Map<String, Object> japKorAlbumLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Album> japKorAlbums = new HashSet<Album>();
		for (Album album : albumService.findAllAlbums()) {
			for (Singer singer : album.getSingers())
				if (singer.getNationality().equals("日本") || singer.getNationality().equals("韩国")) {
					japKorAlbums.add(album);
					break;
				}
		}

		values.put("albums", japKorAlbums);
		return values;
	}

	@RequestMapping("/commentSong")
	public Map<String, Object> commentSong(@RequestBody Comment comment) throws Exception {
		Map<String, Object> values = new HashMap();
		Integer songId = comment.getId();
		Set<Comment> comments = new HashSet();
		commentService.addComment(comment);
		for (Comment c : commentService.findAllComments())
			if (c.getSong().getId().equals(songId))
				comments.add(c);
		values.put("comments", comments);
		return values;
	}

	@RequestMapping("/songPlay")
	public String songPlayer(@RequestParam Integer id, Model model,HttpSession session) throws Exception {

		Song song = songService.findSongById(id);

		Set<Picture> pictures = pictureService.findAllPicturesOfBT();
		Set<Album> albums = song.getAlbums();
		Set<Album> album = new HashSet<Album>();
		for (Album a : albums) {
			album.add(albumService.findAlbumById(a.getId()));
			break;
		}
		Set<Comment> allComments = commentService.findAllComments();
		Set<Comment> comments = new HashSet();
		for (Comment comment : allComments)
			if (comment.getSong().getId().equals(song.getId()))
				comments.add(comment);
		for (Comment c : comments)
			c.setUser(userService.findUserById(c.getUser().getId()));
		song.setAlbums(album);

		model.addAttribute("song", song);
		model.addAttribute("comments", comments);
		model.addAttribute("user",(User)session.getAttribute("USER"));
		return "songPlay";
	}

	@RequestMapping("/mvPlay")
	public String mvPlayer(@RequestParam Integer id, Model model,HttpSession session) throws Exception {
		Mv mv = mvService.findMvById(id);
		model.addAttribute("mv", mv);
		model.addAttribute("user",(User)session.getAttribute("USER"));
		return "mvPlay";
	}

	@RequestMapping("/singerInfo")
	public String singerInfo(@RequestParam Integer id, Model model,HttpSession session) throws Exception {
		Singer singer = singerService.findSingerById(id);
		model.addAttribute("songs", singer.getSongs());
		Set<Song> newSongs = new HashSet();
		for (Song song : singer.getSongs()) {
			if(song!=null) {
			Song newSong = songService.findSongById(song.getId());
			if(newSong.getMv()!=null) {
			newSong.setMv(mvService.findMvById(newSong.getMv().getId()));
			newSongs.add(newSong);
			}
			}
		}
		singer.setSongs(newSongs);
		Set<Album> newAlbums = new HashSet();
		for (Album album : singer.getAlbums()) {
			newAlbums.add(albumService.findAlbumById(album.getId()));
		}
		singer.setAlbums(newAlbums);
		model.addAttribute("singer", singer);
		model.addAttribute("user",(User)session.getAttribute("USER"));
		return "singerInfoPage";
	}

	@RequestMapping("/combinationInfo")
	public String combinationInfo(@RequestParam Integer id,Model model,HttpSession session) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		Set<Song> newSongs = new HashSet();
		Combination combination = combinationService.findCombinationById(id);
		Set<Album> newAlbums = new HashSet();
		for (Song song : combination.getSongs()) {
			System.out.println(song);
			if(song!=null) {
			Song newSong = songService.findSongById(song.getId());
			if(newSong!=null) {
				System.out.println(newSong);
			newSong.setMv(mvService.findMvById(newSong.getMv().getId()));
			newSongs.add(newSong);
			}
			}
		}
		combination.setSongs(newSongs);
		for (Album album : combination.getAlbums()) {
			newAlbums.add(albumService.findAlbumById(album.getId()));
		}
		System.out.println(newAlbums);
		combination.setAlbums(newAlbums);
		model.addAttribute("combination", combination);
		model.addAttribute("user",(User)session.getAttribute("USER"));
		return "combinationInfoPage";
	}

	@RequestMapping("/albumInfo")
	public String albumInfo(@RequestParam Integer id, Model model,HttpSession session) throws Exception {
		Album album = albumService.findAlbumById(id);
		model.addAttribute("album", album);
		model.addAttribute("user",(User)session.getAttribute("USER"));
		return "albumInfoPage";
	}

	@RequestMapping("/userInfo")
	public String userInfo(@RequestParam Integer id, Model model,HttpSession session) throws Exception {
		User user = userService.findUserById(id);
		Set<Songlist> newSonglist = new LinkedHashSet();
		for (Songlist songlist : user.getLikeSonglists()) {
			Songlist songlist1 = songlistService.findSonglistById(songlist.getId());
			int i = 1;
			Set<Song> newSong = new LinkedHashSet();
			for (Song song : songlist1.getSongs()) {
				Song song1 = songService.findSongById(song.getId());
				Set<Album> newAlbum = new LinkedHashSet();
				for (Album album : song1.getAlbums()) {
					if (i == 1) {
						Album album1 = albumService.findAlbumById(album.getId());
						newAlbum.add(album1);
						song1.setAlbums(newAlbum);
						i = 2;
					}
				}
				song = song1;
				newSong.add(song);
			}
			songlist1.setSongs(newSong);
			newSonglist.add(songlist1);
		}
		user.setLikeSonglists(newSonglist);

		newSonglist = new LinkedHashSet();
		for (Songlist songlist : user.getOwnedSonglists()) {
			Songlist songlist1 = songlistService.findSonglistById(songlist.getId());
			int i = 1;
			Set<Song> newSong = new LinkedHashSet();
			for (Song song : songlist1.getSongs()) {
				Song song1 = songService.findSongById(song.getId());
				Set<Album> newAlbum = new LinkedHashSet();
				for (Album album : song1.getAlbums()) {
					if (i == 1) {
						Album album1 = albumService.findAlbumById(album.getId());
						newAlbum.add(album1);
						song1.setAlbums(newAlbum);
						i = 2;
					}
				}
				song = song1;
				newSong.add(song);
			}
			songlist1.setSongs(newSong);
			newSonglist.add(songlist1);
		}
		user.setOwnedSonglists(newSonglist);
		model.addAttribute("user", user);
		return "userInfoPage";
	}

	@RequestMapping("/userInfoEdit")
	@ResponseBody
	public Map<String, Object> userInfoEdit(@RequestBody Integer id) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		User user = userService.findUserById(id);
		values.put("user", user);
		return values;
	}

	@RequestMapping("/likeSongList")
	@ResponseBody
	public Map<String, Object> likeSongList(@RequestBody Integer id) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		User user = userService.findUserById(id);
		values.put("likeSongLists", user.getLikeSonglists());
		return values;
	}

	@RequestMapping("/ownedSongList")
	@ResponseBody
	public Map<String, Object> ownedSongList(@RequestBody Integer id) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		User user = userService.findUserById(id);
		values.put("ownedSongLists", user.getOwnedSonglists());
		return values;
	}

	@RequestMapping("/newSongLists")
	@ResponseBody
	public Map<String, Object> newSongLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("songLists", songService.findNewSongs());
		return values;
	}

	@RequestMapping("/hotSongLists")
	@ResponseBody
	public Map<String, Object> hotSongLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("songLists", songService.findHotSongs());
		return values;
	}

	@RequestMapping("/newAlbumLists")
	@ResponseBody
	public Map<String, Object> newAlbumLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("albums", albumService.findNewAlbums());
		return values;
	}

	@RequestMapping("/hotMvLists")
	@ResponseBody
	public Map<String, Object> hotAlbumLists() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		List<Song> songs = songService.findHot4Songs();
		List<Mv> mvs = new ArrayList();
		for (Song song : songs) {
			if (song.getMv() != null) {
				mvs.add(mvService.findMvWPById(song.getMv().getId()));
			}
		}
		values.put("mvs", mvs);
		return values;
	}

	@RequestMapping("/modifyUser")
	@ResponseBody
	public Boolean modifyUser(@RequestBody User user) throws Exception {
		User newUser = userService.findUserById(user.getId());
		newUser.setUsername(user.getUsername());
		newUser.setNickname(user.getNickname());
		newUser.setPhone(user.getPhone());
		newUser.setEmail(user.getEmail());

		Picture picture = user.getPicture();
		if (picture.getId().equals(-1)) {
			pictureService.addPicture(picture);
			newUser.setPicture(picture);
		}

		userService.updateUser(newUser);
		return true;
	}
}
